<template>
  <a-card class="body-container">
    <s-tool-bar>
      <template #left>
        <s-button
          v-if="authorize.insert"
          type="primary"
          icon="plus-outlined"
          @click="$refs.add.open()"
          >新增</s-button
        >
        <a-popconfirm
          v-if="authorize.deleteMulti"
          title="确认删除所选数据？"
          ok-text="是"
          cancel-text="否"
          @confirm="deleteMulti"
        >
          <s-button type="danger" icon="delete-outlined">删除</s-button>
        </a-popconfirm>
      </template>
      <template #right>
        <s-button type="primary" icon="redo-outlined" @click="$refs.table.refresh()">刷新</s-button>
      </template>
    </s-tool-bar>
    <s-table
      ref="table"
      bordered
      :scroll="{ y: tableHeight }"
      :columns="columns"
      url="/code/model/pageList"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <a
            v-if="authorize.updateModelForm"
            style="margin-right: 8px"
            @click="$refs.update.open(record.id)"
          >
            <s-icon type="edit-outlined" />编辑
          </a>
          <a
            v-if="authorize.updateModelColumn"
            style="margin-right: 8px"
            @click="$parent.openColumn(record.id)"
          >
            <s-icon type="table-outlined" />列配置
          </a>
          <a v-if="authorize.syncTable" style="margin-right: 8px" @click="syncTable(record.id)">
            <s-icon type="retweet-outlined" />数据库同步
          </a>
          <a-popconfirm
            v-if="authorize.delete"
            title="确认删除此条数据？"
            ok-text="是"
            cancel-text="否"
            @confirm="deleteById(record.id)"
          >
            <a style="margin-right: 8px"> <s-icon type="delete-outlined" />删除 </a>
          </a-popconfirm>
          <a v-if="authorize.update" style="margin-right: 8px" @click="genCode(record.id)">
            <s-icon type="retweet-outlined" />数据库同步
          </a>
        </template></template
      >
    </s-table>
    <add
      ref="add"
      :items="formItems"
      :init-value="initValue"
      @success="
        () => {
          $refs.table.refresh()
        }
      "
    />
    <update
      ref="update"
      :items="formItems"
      @success="
        () => {
          $refs.table.refresh()
        }
      "
    />
  </a-card>
</template>

<script>
import Add from '.././add/index.vue'
import Update from '.././update/index.vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'
export default {
  name: 'QueryList',
  components: { Add, Update },
  data() {
    return {
      query: {},
      columns: [],
      formItems: [],
      initValue: {}
    }
  },
  authorize: '/code/model/permissionList',
  created() {
    this.$api.GET('/code/config/selectOne').then((res) => {
      this.initValue = res.data.testData.model
      this.formItems = res.data.formConfig.model.form
      res.data.formConfig.model.list.forEach((item) => {
        let newItem = JSON.parse(JSON.stringify(item))
        newItem.dataIndex = newItem.dataIndex.split('.')
        this.columns.push(newItem)
      })
      this.columns.push({
        title: '操作',
        fixed: 'right',
        key: 'action',
        width: 320
      })
    })
  },
  methods: {
    syncTable(id) {
      const that = this
      this.$confirm({
        title: '提示',
        content: '同步数据库将会删除所有的数据，确认执行吗？',
        okText: '确认',
        cancelText: '取消',
        onOk: function () {
          that.$loading(true)
          that.$api
            .GET('/code/model/syncTable', { id: id })
            .then((res) => {
              that.$loading(false)
              that.$message.info(res.msg)
            })
            .catch(() => {
              that.$loading(false)
            })
        }
      })
    },
    deleteMulti() {
      const keys = this.$refs.table.getSelectedKeys()
      if (keys.length > 0) {
        this.$loading(true)
        this.$api
          .DELETE('/code/model/deleteMulti', { ids: keys.toString() })
          .then(() => {
            this.$loading(false)
            this.$refs.table.search(this.query)
          })
          .catch(() => {
            this.$loading(false)
          })
      }
    },
    deleteById(id) {
      this.$loading(true)
      this.$api
        .DELETE('/code/model/delete', { id: id })
        .then(() => {
          this.$loading(false)
          this.$refs.table.search(this.query)
        })
        .catch(() => {
          this.$loading(false)
        })
    }
  }
}
</script>

<style lang="less" scoped></style>
